package com.yanqu.road.server.cmd.orewar;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.enums.ePatronsSkillType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.cmd.orewar.task.RobPitTask;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.OreWarModule;
import com.yanqu.road.server.logic.battle.BattleManager;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.S_ORE_WAR_ROB_PIT, desc = "抢夺矿坑")
public class OrePitRobCmd extends AbstractCommand {

	private int activityId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		OreWarProto.OrePitRobReqMsg reqMsg = OreWarProto.OrePitRobReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		ActivityInfo activityInfo = OreWarActivityMgr.getInTimeActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_ORE_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(!OreWarMgr.isActivityActive(activityId)){
			ret = GameErrorCode.E_ORE_ACTIVITY_NO_IN_DAY_TIME;
			return;
		}
		if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
			OreWarMgr.addTask(activityId,new RobPitTask(packet, player));
		}else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
			int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
			if (groupId == UnionActivityGroupHelper.NO_GROUP_ID){
				ret = GameErrorCode.E_ORE_WAR_CROSS_NO_GROUP;
				return;
			}

			ret = OreWarMgr.checkCrossRobPit(activityId,player,reqMsg.getPitId(),reqMsg.getPatronsIdListList(),reqMsg.getPitOwnerUserId(),groupId);
			if(ret == 0){
				OreWarProto.CrossOrePitRobReqMsg.Builder msg = OreWarProto.CrossOrePitRobReqMsg.newBuilder();
				msg.setActivityId(reqMsg.getActivityId());
				msg.setPitId(reqMsg.getPitId());
				msg.setPitOwnerUserId(reqMsg.getPitOwnerUserId());
				List<UserPatrons> patronsList = player.getModule(PatronsModule.class).getUserPatronsList(reqMsg.getPatronsIdListList());
				patronsList = BattleManager.getCloneSkillOreWarAddPatrons(patronsList);
				msg.setPatronsInfoStr(JSON.toJSONString(patronsList));
				msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
				int beautySkillAddition = player.getModule(BeautyModule.class).getIdentitySkillAddition(eSkillType.MineOutput.getValue(), 0);
				int patronsSkillAddition = 0;
				for(UserPatrons userPatrons : patronsList){//计算天赋加成
					for(UserPatronsSkill patronsSkill : userPatrons.getSkillList()){
						if(patronsSkill.getType() != ePatronsSkillType.Talent.getValue()){
							continue;
						}
						SkillInfo skillInfo = SkillMgr.getSkillInfo(patronsSkill.getSkillId());
						if(skillInfo.getType() == eSkillType.MineWarAddOutput.getValue()){
							patronsSkillAddition += SkillMgr.getSkillAddition(skillInfo, patronsSkill.getSkillLv());
						}
					}
				}
				msg.setBeautySkillAddition(beautySkillAddition);
				msg.setPatronsSkillAddition(patronsSkillAddition);
				msg.setRefugeCount(player.getModule(OreWarModule.class).getRefugeCount(activityId));
				GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_OREWAR_ROBPIT,msg,player.getUserId(),activityId,groupId);

			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			OreWarProto.OrePitRobRespMsg.Builder resMsg = OreWarProto.OrePitRobRespMsg.newBuilder();
			resMsg.setRet(ret);
			player.sendPacket(Protocol.U_ORE_WAR_ROB_PIT,resMsg);
		}
	}
}
